/* $Id: BuchholzRankingComparator.java 90 2009-05-08 21:39:48Z henk $ */
package org.jugger.tournament.model.ranking;

import org.jugger.tournament.model.Stats;
import org.jugger.tournament.model.TeamStats;

import util.I18N;


/**
 * @author Sven Hamer
 */
public class BuchholzRankingComparator extends RankingComparator {

	private static String[] columnNames = { "Buchholz" };

	@Override
	public int getAdditionalColumnCount() {
		return columnNames.length;
	}

	@Override
	public String getAdditionalColumnName(int columnIndex) {
		return columnNames[columnIndex];
	}

	@Override
	public Object getAdditionalColumnValue(TeamStats teamStats, int columnIndex) {
		switch(columnIndex) {
		case 0:
			return teamStats.getOpponentsStats().getScorePositive();
		default:
			return null;
		}
	}

	/**
	 * Compares two team stats based on Buchholz ranking.
	 */
	public int compare(TeamStats teamStats1, TeamStats teamStats2) {

		Stats stats1 = teamStats1.getStats();
		Stats stats2 = teamStats2.getStats();

		// score
		if(stats1.getScorePositive() < stats2.getScorePositive())
			return 1;
		if(stats1.getScorePositive() > stats2.getScorePositive())
			return -1;

		Stats opponentsStats1 = teamStats1.getOpponentsStats();
		Stats opponentsStats2 = teamStats2.getOpponentsStats();

		// buchholz => score of all opponents
		if(opponentsStats1.getScorePositive() < opponentsStats2.getScorePositive())
			return 1;
		if(opponentsStats1.getScorePositive() > opponentsStats2.getScorePositive())
			return -1;

		// points difference
		if(stats1.getPointsDifference() < stats2.getPointsDifference())
			return 1;
		if(stats1.getPointsDifference() > stats2.getPointsDifference())
			return -1;

		// positive points
		if(stats1.getPointsPositive() < stats2.getPointsPositive())
			return 1;
		if(stats1.getPointsPositive() > stats2.getPointsPositive())
			return -1;

		// negative points
		if(stats1.getPointsNegative() > stats2.getPointsNegative())
			return 1;
		if(stats1.getPointsNegative() < stats2.getPointsNegative())
			return -1;

		// same rank => first is better.
		return 1;
	}

	@Override
	public String toString() {
		return I18N.__("descriptionBuchholzRankingComparator");
	}

	// register in instances table
	static {
		instances.put(BuchholzRankingComparator.class, new BuchholzRankingComparator());
	}
}
